<?php
class HomeModel extends Model {
	protected $redis;
	protected $db;
	protected $type;
	protected $location;
	protected $url = "http://test.shiangou.com.cn";
	public function __construct(){
		//连接本地的 Redis 服务
		$redis = new Redis();
		$redis->connect('127.0.0.1', 6379);
		$redis -> flushAll();
		$this->redis = $redis;
		$this->db = D('Merchant');
		$this->type = D('Merchant_type');
		$this->location = D('MerchantLocation');
	}
	public function getList(){
		$data = $this
		->db
		->join(C('DB_PREFIX').'merchant_location ON '.C('DB_PREFIX').'merchant.merchant_id = '.C('DB_PREFIX').'merchant_location.merchant_id')
		->select();
		return $data;
	}
	public function each($data){
		foreach($data as $key=> $values){
			$this->redis->set($values['merchant_id'],$values['coordinate']);  
		}
	}

	  //计算两点之间距离 
	    /**
		 * 计算两个坐标之间的距离(米)
		 * @param float $fP1Lat 起点(纬度)
		 * @param float $fP1Lon 起点(经度)
		 * @param float $fP2Lat 终点(纬度)
		 * @param float $fP2Lon 终点(经度)
		 * @return int
		 */

	public function distanceBetween($fP1Lat, $fP1Lon, $fP2Lat, $fP2Lon){
	    $fEARTH_RADIUS = 6378137;
	    //角度换算成弧度
	    $fRadLon1 = deg2rad($fP1Lon);
	    $fRadLon2 = deg2rad($fP2Lon);
	    $fRadLat1 = deg2rad($fP1Lat);
	    $fRadLat2 = deg2rad($fP2Lat);
	    //计算经纬度的差值
	    $fD1 = abs($fRadLat1 - $fRadLat2);
	    $fD2 = abs($fRadLon1 - $fRadLon2);
	    //距离计算
	    $fP = pow(sin($fD1/2), 2) +
	          cos($fRadLat1) * cos($fRadLat2) * pow(sin($fD2/2), 2);
	    return intval($fEARTH_RADIUS * 2 * asin(sqrt($fP)) + 0.5)/1000;
	}


	public function coordinate($location,$lat,$lon){
		foreach($location as $k=>$val){
			$arr = explode(',',$this->resGet($val['merchant_id']));
			$distance = $this->distanceBetween($lat,$lon,$arr[1],$arr[0]);
			if( $distance < 20){
				$coordinate[$k]['between'] = $distance;
				$coordinate[$k]['merchant_id'] =  $val['merchant_id'];
				$coordinate[$k]['coordinate'] = $this->resGet($val['merchant_id']);
				$coordinate[$k]['grade'] = $val['grade'];
			}
		}
		return $coordinate;
	}
	public function resGet($key){
		$data = $this
		->redis
		->get($key);
		return $data;
	}
	public function getInfo($merchant_id){
		$data = $this->redis->lRange($merchant_id, 0, -1);
		return $data;
	}
	public function oneList($merchant_id){
		$where = array('merchant_id' => $merchant_id);
		$data = $this
		->db
		->field('merchant_id, merchant_name, grade, address, merchant_thumb')
		->where($where)
		->find();
		return $data;
	}
	public function sel(){
		$data = $this
		->db
		->select();
		return $data;
	}
	public function type($parent){
		$where = array('type_parent' => $parent);
		$data = $this
		->type
		->field('type_id,type_name,type_img')
		->where($where)
		->select();
		$return = $this->host($data);
		return $return;
	}
	public function host($data){
		foreach($data as $vals){
			$vals['type_img'] = $this->url.$vals['type_img'];
			$arr[] = $vals;
		}
		return $arr;
	}

	public function ArrGet($data){
		foreach($data as $vals){
			$arr[] = $this->location_find($vals['merchant_id']);
		}
		return $arr;
	}
	public function Merchant_type($type){
		$where = array('type_parent' => $type);
		$data = $this
		->db
		->where($where)
		->select();
		return $data;
	}
	public function location_find($merchant_id){
		$where = array('merchant_id'=>$merchant_id);
		$data  = $this
		->location
		->where($where)
		->find();
		return $data;
	}
	public function category($type_parent){
		$where = array('type_parent' => $type_parent);
		$data = $this
		->type
		->field('type_id as cat_id,type_name as cat_name')
		->where($where)
		->select();
		return $data;
	}
	public function stage(){
		$type_id = I('post.type_id',1);//大分类
		$cat_id  = I('post.cat_id',0);
		$grade   = I('post.grade');
		$lat = I('post.lat',39.827342987060547);
		$lon = I('post.lon',116.28915405273438);
		$curpage = I('post.curpage',1);
		$where   = "";
		$order   = "grade asc";
		if($type_id){
			$where .= "merchant_type = $type_id or merchant_type in (select type_id from manage_merchant_type where type_parent = $type_id)";
		}
		if($cat_id){
			$where = "merchant_type = $cat_id";
		}
		if($grade == 1){
			$order = "grade desc";
		}
		if($cat_id == 0){
			$where = "merchant_type = $type_id or merchant_type in (select type_id from manage_merchant_type where type_parent = $type_id)";
		}
		$data = $this
		->db
		->where($where)
		->order($order)   
		->select();
		$array = $this->getList();
		$this->each($array);
		$return = $this->coordinate($data,$lat,$lon);
		$stage = $this->forIn($return);
		if($grade == 0){
			array_multisort(array_column($stage,'between'),SORT_ASC,$stage);
		}
		$page = ($curpage-1)*10;
		$slice = array_slice($stage,$page,10);
		return $slice;
	}

	public function forIn($return){
		foreach($return as $key=>$vals){
			$arr[$key]= $this->oneList($vals['merchant_id']);
			$arr[$key]['merchant_thumb'] = $this->url.$arr[$key]['merchant_thumb'];
			$arr[$key]['between'] =$vals['between'];
		}
		return $arr;
	}
	public function like(){
		$like = I('post.keywords');
		$lat  = I('post.lat');
		$lon  = I('post.lon');
		$curpage = I('post.curpage',1);
		$where['merchant_name']  = array('like', "%$like%");
		$where['address']  = array('like',"%$like%");
		$where['_logic'] = 'or';
		$map['_complex'] = $where;
		$array = $this->getList();
		$this->each($array);
		$data = $this
		->db
		->field('merchant_id, merchant_name, grade, address, merchant_thumb')
		->where($map)
		->select();
		$return = $this->coordinate($data,$lat,$lon);
		$stage = $this->forIn($return);
		array_multisort(array_column($stage,'between'),SORT_ASC,$stage);
		$page = ($curpage-1)*10;
		$slice = array_slice($stage,$page,10);
		return $slice;
	}
}
